package com.cqc.googleplay.view.part;

import java.text.Format;
import java.util.ArrayList;

import com.cqc.googleplay.R;
import com.cqc.googleplay.application.ImageLoaderOptions;
import com.cqc.googleplay.application.MyApplication;
import com.cqc.googleplay.bean.AppInfoBean;
import com.cqc.googleplay.bean.SafeInfo;
import com.cqc.googleplay.http.NetUrl;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AppSafePart implements OnClickListener {

	private Context context;
	private View part2View;
	private LinearLayout ll1_part2;
	private ImageView iv_arrow_part2;
	private ImageView iv1_ll1_part2;
	private ImageView iv2_ll1_part2;
	private ImageView iv3_ll1_part2;
	private LinearLayout ll2_part2;
	private LinearLayout ll3_part2;
	private LinearLayout ll4_part2;
	private LinearLayout ll5_part2;
	private ImageView iv_ll3_part2;
	private ImageView iv_ll4_part2;
	private ImageView iv_ll5_part2;
	private TextView tv_ll3_part2;
	private TextView tv_ll4_part2;
	private TextView tv_ll5_part2;

	public AppSafePart() {
		super();
		context = MyApplication.getContext();
	}

	public View getView() {
		part2View = View.inflate(context, R.layout.part2_app_safe, null);
		findViews();
		ll1_part2.setOnClickListener(this);
		return part2View;
	}

	private void findViews() {
		ll1_part2 = (LinearLayout) part2View.findViewById(R.id.ll1_part2);
		iv_arrow_part2 = (ImageView) part2View.findViewById(R.id.iv_arrow_part2);

		iv1_ll1_part2 = (ImageView) part2View.findViewById(R.id.iv1_ll1_part2);
		iv2_ll1_part2 = (ImageView) part2View.findViewById(R.id.iv2_ll1_part2);
		iv3_ll1_part2 = (ImageView) part2View.findViewById(R.id.iv3_ll1_part2);

		ll2_part2 = (LinearLayout) part2View.findViewById(R.id.ll2_part2);
		ll3_part2 = (LinearLayout) part2View.findViewById(R.id.ll3_part2);
		ll4_part2 = (LinearLayout) part2View.findViewById(R.id.ll4_part2);
		ll5_part2 = (LinearLayout) part2View.findViewById(R.id.ll5_part2);

		iv_ll3_part2 = (ImageView) part2View.findViewById(R.id.iv_ll3_part2);
		iv_ll4_part2 = (ImageView) part2View.findViewById(R.id.iv_ll4_part2);
		iv_ll5_part2 = (ImageView) part2View.findViewById(R.id.iv_ll5_part2);

		tv_ll3_part2 = (TextView) part2View.findViewById(R.id.tv_ll3_part2);
		tv_ll4_part2 = (TextView) part2View.findViewById(R.id.tv_ll4_part2);
		tv_ll5_part2 = (TextView) part2View.findViewById(R.id.tv_ll5_part2);
	}

	public void setData(AppInfoBean info) {
		ArrayList<SafeInfo> safeList = info.getSafe();

		// 设置第一行
		SafeInfo safeInfo1 = safeList.get(0);
		ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + safeInfo1.getSafeUrl(), iv1_ll1_part2, ImageLoaderOptions.pagerOptions);
		ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + safeInfo1.getSafeDesUrl(), iv_ll3_part2, ImageLoaderOptions.pagerOptions);
		tv_ll3_part2.setText("" + safeInfo1.getSafeDes());

		// 设置第二行
		if (safeList.size() > 1) {
			ll4_part2.setVisibility(View.VISIBLE);
			SafeInfo safeInfo2 = safeList.get(1);
			ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + safeInfo1.getSafeUrl(), iv2_ll1_part2, ImageLoaderOptions.pagerOptions);
			ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + safeInfo1.getSafeDesUrl(), iv_ll4_part2, ImageLoaderOptions.pagerOptions);
			tv_ll4_part2.setText("" + safeInfo2.getSafeDes());
		} else {
			ll4_part2.setVisibility(View.GONE);
		}
		// 设置第三行
		if (safeList.size() > 2) {
			ll5_part2.setVisibility(View.VISIBLE);
			SafeInfo safeInfo3 = safeList.get(2);
			ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + safeInfo1.getSafeUrl(), iv3_ll1_part2, ImageLoaderOptions.pagerOptions);
			ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + safeInfo1.getSafeDesUrl(), iv_ll5_part2, ImageLoaderOptions.pagerOptions);
			tv_ll5_part2.setText("" + safeInfo3.getSafeDes());
		} else {
			ll5_part2.setVisibility(View.GONE);
		}

		// 设置值动画
		// 1求出 ll2_part2 本来的高度
		ll2_part2.measure(0, 0);
		maxHeight = ll2_part2.getMeasuredHeight();
		// 2 隐藏ll2_part2，将高度设置0
		ll2_part2.getLayoutParams().height = 0;
		ll2_part2.requestLayout();

		//设置从左向右出现
		WindowManager windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
		int width = windowManager.getDefaultDisplay().getWidth();
		ViewHelper.setTranslationX(ll1_part2, -width);
		ViewPropertyAnimator.animate(ll1_part2)
			.setDuration(5000)
//			.setInterpolator(new OvershootInterpolator(5))//弹性的插值器,参数是次数
			.setInterpolator(new BounceInterpolator())//球落地的插值器
//			.setInterpolator(new CycleInterpolator(1))//左右两边的插值器，一次包含：向左一次 + 向右一次，最后回到复位。要注释掉ViewHelper.setTranslationX(ll1_part2, -width);
			.translationXBy(width)		
			.setStartDelay(500)
			.start();
	}

	private boolean flag = false;// 默认隐藏，不显示出来
	private int maxHeight;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll1_part2:
			ValueAnimator valueAnimator = null;
			if (flag) {
				valueAnimator = valueAnimator.ofInt(maxHeight, 0);
			} else {
				valueAnimator = valueAnimator.ofInt(0, maxHeight);
			}
			valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

				@Override
				public void onAnimationUpdate(ValueAnimator valueAnimator) {
					int value = (Integer) valueAnimator.getAnimatedValue();
					ll2_part2.getLayoutParams().height = value;
					ll2_part2.requestLayout();
				}
			});
			valueAnimator.addListener(listener);
			valueAnimator.setDuration(300).start();
			break;
		}
	}

	private AnimatorListener listener = new AnimatorListener() {

		@Override
		public void onAnimationStart(Animator animator) {
			// 直接设置前景色/背景色 没有动画效果
//			iv_arrow_part2.setImageResource(flag? R.drawable.arrow_up :R.drawable.arrow_down);
			ViewPropertyAnimator.animate(iv_arrow_part2).rotationBy(180).setDuration(300).start();
			// rotation(180)只转一次
//			ViewPropertyAnimator.animate(iv_arrow_part2)
//				.setDuration(350)
//				.rotationBy(180)
//				.setInterpolator(new CycleInterpolator(3))//执行3次动画
//				.start();
		}

		@Override
		public void onAnimationRepeat(Animator animator) {
		}

		@Override
		public void onAnimationEnd(Animator animator) {
			flag = !flag;
		}

		@Override
		public void onAnimationCancel(Animator animator) {
		}
	};
}
